package com.zhangchuchu.neohealth.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangchuchu.neohealth.entity.Article;
import com.zhangchuchu.neohealth.mapper.ArticleMapper;
import com.zhangchuchu.neohealth.service.ArticleService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Override
    public IPage<Article> getArticles(int page, int size) {
        Page<Article> pageParam = new Page<>(page, size);
        return baseMapper.selectPage(pageParam, null);
    }

    @Override
    public List<Article> getTop4Articles() {
        return baseMapper.getTop4ArticlesByLikes();
    }
    @Override
    public boolean likeArticle(Integer userId, Integer articleId) {
        // 检查用户是否已经点赞
        int count = baseMapper.checkUserLiked(userId, articleId);
        if (count > 0) {
            return false; // 用户已经点过赞
        }

        // 插入点赞记录
        baseMapper.insertLike(userId, articleId);

        // 更新文章点赞数
        baseMapper.incrementLikes(articleId);

        return true;
    }

    @Override
    public Article getById(Integer id) {
        return baseMapper.selectById(id);
    }

}
